import numpy as np
import pylab as pl
def sine_wave(ampl=1, fc=1000, phase=0, fs=32000, duration=0.01):
    # 时间序列
    t = np.arange(duration*fs)/fs    
    # 等同于 np.arange(0, duration, 1/fs)

    wave = ampl * np.exp(1j*(2 * np.pi * fc * t + phase))
    phs_end = phase + 2 * np.pi * fc * duration  # 末相位
    return wave, phs_end

def plot_wave(x, fs=32000, display=True, hold=False, title=''):
    x = np.array(x)
    # 1 计算时间轴序列 t 
    t = np.arange(x.shape[0]) / fs

    # 2 plot
    if display:
        if not hold:
            pl.figure(figsize=(15, 2))
        if np.isreal(x[0] and x[1] and x[2]):  # 用前3个数作数实数/复数判断的标准
            pl.plot(t, x)
        else:
            pl.plot(t, x.real, label="I")
            pl.plot(t, x.imag, '--', label="Q")
            pl.legend(loc=1)

        pl.xlim([t[0], t[-1] + t[1] - t[0]])
        pl.xlabel('Time (s)')

        pl.ylabel('AMP')
        if title:
            pl.title(title)
        pl.tight_layout()
        return t, x
    else:
        return t, x  # 只要数据不画图
wave=sine_wave()[0]
plot_wave(wave)